package com.example.administrator.text;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.google.gson.Gson;
import java.util.List;
import java.util.Map;
import adapter.RecyclerHome;
import app.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import entity.Account;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.LogUtil;
import util.NetUtils;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.UploadDialog;

/**
 * 显示相关的账号信息  就是账号列表1
 */
public class AccountListActivity extends BaseActivity {
    List<Account.AreaInfoBean> mAreaInfoBeen;
    RecyclerHome mRecyclerHome;
    UploadDialog mUploadDialog;
    public static final String AREAID = "AREAID";      //可绑定区域id
    public static final String AREIDEN = "AREIDEN";   //可绑定区域类型标示
    public static final String AREANAME = "AREANAME";//区域名
    public static final String AREAPIC = "AREAPIC"; //区域图标
    public static final String PWSNAME = "pwdName"; //密码的提示
    public static final String ACCOUNANME = "accountName"; //账号的提示

    @Bind(R.id.recyler_accountlist)  RecyclerView mRecyclerView;
    @Bind(R.id.iv_accountlist_fail)  ImageView mImageView;
    @Bind(R.id.enter_accountlist1)    FrameLayout mFrameLayout;
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accountlist);
        ButterKnife.bind(this);
        initData();
        HomeUtil.getHemeUtilNew().getStatusBar(this,mFrameLayout);
    }

    /**
     * 返回
     */
    @OnClick(R.id.image_accountlist) void getReturn(){finish();}


    private void initData() {
        String url = "";
        String stringJson ="{}"; //参数包
        mUploadDialog = new UploadDialog(this,"加载中");
        mUploadDialog.show();
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String token = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mTokenDB, "");
        String id = (String) SharedPreferencesUtils.getParam(this,HomeUtil.mUseridDB,"");
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngBinding_areaList,stringJson,token,id,time);
        Map map = StringUtil.getStringUtilNew().getMapNew(this,stringJson,token,id,Url.mStirngBinding_areaList,stirngSign,time);
        if(NetUtils.getInstance(this).getNetWorkTeyt() == 0){
            url = Url.getUrlNew().getAppIP();
        }else {
            url = (String) SharedPreferencesUtils.getParam(this,HomeUtil.mFWQ,"");
        }
        getLogger().info("initData()--->账号列表--fwq--"+url+"stirngSign"+stirngSign);
        GreenTreeNetworkUtil.getInstance().doPost(this,url,1,map, new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                Gson gson = new Gson();
                LogUtil.eE("JSON","initData"+response);
                getLogger().info("initData()--->onSuccess()--String"+response);
                mUploadDialog.dismiss();
                mImageView.setVisibility(View.GONE);
                Account account = gson.fromJson(response,Account.class);

                if(account.getCode() == 1){
                    mAreaInfoBeen = account.getAreaInfo();
                } else if (account.getCode() == -2) {
                    CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(AccountListActivity.this);
                    crowdRetunDialog.show();
                }
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AccountListActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                mRecyclerView.setLayoutManager(linearLayoutManager);
                mRecyclerHome = new RecyclerHome(AccountListActivity.this,null,null,mAreaInfoBeen,4);
                mRecyclerView.setAdapter(mRecyclerHome);
                mRecyclerHome.setOnItemClickListener(new RecyclerHome.OnRecyclerViewItemClickListener() {
                    @Override public void onItemClick(int position,String type) {
                        Intent intent = new Intent();
                        intent.putExtra(AREAID,mAreaInfoBeen.get(position).getBindingareaid());
                        intent.putExtra(AREIDEN,mAreaInfoBeen.get(position).getAreaiden());
                        intent.putExtra(AREANAME,mAreaInfoBeen.get(position).getAreaName());
                        intent.putExtra(AREAPIC,mAreaInfoBeen.get(position).getAreaPic());
                        intent.putExtra(PWSNAME,mAreaInfoBeen.get(position).getPwdName());
                        intent.putExtra(ACCOUNANME,mAreaInfoBeen.get(position).getAccountName());
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });
            }

            @Override public void onFailure(Throwable error) {
                mUploadDialog.dismiss();
                mImageView.setVisibility(View.VISIBLE);
                mImageView.setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        initData();
                    }
                });
                StringUtil.getStringUtilNew().getExceptionCode(AccountListActivity.this,error.toString());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mUploadDialog !=null){mUploadDialog.dismiss();}

        if(mAreaInfoBeen !=null){mAreaInfoBeen.clear();}
    }
}
